package com.disgruntledbots;

import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.constants.PhysicsConstants;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.IDisposable.AlreadyDisposedException;

import android.util.Log;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class PhysicsEntityConnector implements PhysicsConstants{
	public Sprite mSprite;
	public Body mBody;
	public PhysicsConnector mConnector;
	private PhysicsWorld pWorld;
	public String name;
	
	
	//static float PIXEL_TO_METER_RATIO_DEFAULT = 
	
	public PhysicsEntityConnector(float X, float Y, ITextureRegion texture, VertexBufferObjectManager vbom, PhysicsWorld pWorld, 
			float mass, float elasticity, float friction)
	{
		this.pWorld = pWorld;
		this.mSprite = new Sprite(X, Y, texture, vbom);
		this.mSprite.setCullingEnabled(true);
		FixtureDef fixDef = PhysicsFactory.createFixtureDef(getDensity(mass), elasticity, friction);
		this.mBody = PhysicsFactory.createBoxBody(pWorld, this.mSprite, BodyType.DynamicBody, fixDef);
		this.mBody.setSleepingAllowed(true);
		this.mConnector = new PhysicsConnector(mSprite, mBody, true, true);
		pWorld.registerPhysicsConnector(mConnector);
	}
	
	protected float getDensity(float mass)
	{
		float area = ((float)this.mSprite.getHeight()/PIXEL_TO_METER_RATIO_DEFAULT)*
				((float)this.mSprite.getWidth()/PIXEL_TO_METER_RATIO_DEFAULT);
		float density = area/mass;
		return density;
	}
	
	protected void createBody(PhysicsWorld pWorld, FixtureDef fixDef)
	{
		this.mBody = PhysicsFactory.createBoxBody(pWorld, this.mSprite, BodyType.DynamicBody, fixDef);
	}
	
	public void dispose()
	{
		IEntity current = this.mSprite;
		pWorld.unregisterPhysicsConnector(mConnector);
		pWorld.destroyBody(this.mBody);
		Log.w("PhysicsObject", "Dispose: Detatch");
		this.mSprite.detachSelf();
		try{
			while(current.hasParent())
			{
				if(current.getClass() == Scene.class){
					current.detachChild(mSprite);
					Log.w("PhysicsObject", "Dispose: Dispose Called");
					break;
				}else{
					current = current.getParent();
				}
			}
			
		}catch(IndexOutOfBoundsException e){
			
		}
		
		try{
			this.mSprite.dispose();
			Log.w("PhysicsObject", "Dispose: Nullify");
		}catch(AlreadyDisposedException e){
			Log.e("PhysicsEntityConnecter", "Alread Disposed", e);
		}
		
		
		
		this.mBody = null;
		this.mConnector = null;
		this.mSprite = null;
	}
	
	public void setAttributes(String name)
	{
		this.name = name;
	}
}
